package de.bk.timecalc.midlet.statemachine;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import de.bk.timecalc.midlet.Commands;

public class CommandListenerStart extends AbstractCommandListener
{
  CommandListenerStart(GuiStateStart state)
  {
    super(state);
  }

  /**
   * {@inheritDoc}
   * 
   * @see CommandListener#commandAction(Command, Displayable)
   */
  public void commandAction(Command command, Displayable displayable)
  {
    if (this.getState().followTransition(command))
    {
      return;
    }
    else if (command == Commands.exit)
    {
      Alert alertConfirm = new Alert("Confirm", "Are you sure?", null,
          AlertType.CONFIRMATION);
      alertConfirm.setTimeout(Alert.FOREVER);
      alertConfirm.addCommand(Commands.confirmYes);
      alertConfirm.addCommand(Commands.confirmNo);
      alertConfirm.setCommandListener(this);
      this.setCurrent(alertConfirm);
    }
    else if (command == Commands.confirmYes)
    {
      this.getState().getStateMachine().getMidlet().destroy();
    }
    else if (command == Commands.confirmNo)
    {
      this.getState().enter();
    }
    else
    {
      throw new RuntimeException("Unknown command or no transition defined: ["
          + command.getLabel()
          + "]");
    }
  }
}
